package com.lb.shopguide.ui.view.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.lb.shopguide.entity.local.LineBean;
import com.lb.shopguide.util.lb.BizUtil;
import com.lb.shopguide.util.lb.PathAnimHelper;

import java.util.ArrayList;

/**
 * Created by kang on 2018/2/27.
 */

public class LbTwoLineView extends View {
    private int mViewWidth; //view的宽
    private int mViewHeight;    //view的高

    private Paint mBaseLinePaint;
    private Paint mDatePaint;
    private Path mPathMember;
    private Path mPathOrder;
    private Paint mPathMemberPaint;
    private Paint mPathOrderPaint;
    private Path mDisPathMember;
    private Path mDisPathOrder;
    private int colorMember = Color.rgb(40, 180, 250);
    private int colorOrder = Color.rgb(244, 117, 117);

    private int chartHeight = 332;
    private int lineHeight = 272;

    private int amount = 4;
    private int amountWidth;

    private float maxData;

    private ArrayList<LineBean> listMember;
    private ArrayList<LineBean> listOrder;

    private float density;
    private float scaleDensity;

    private PathAnimHelper helperMember;
    private PathAnimHelper helperOrder;

    public LbTwoLineView(Context context) {
        super(context);
    }

    public LbTwoLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        initParams();

        initPaint();

        initPath();
    }


    public LbTwoLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initParams() {
        density = getResources().getDisplayMetrics().density / 2;
        scaleDensity = getResources().getDisplayMetrics().density / 2;

        chartHeight = (int) (chartHeight * density);
        lineHeight = (int) (lineHeight * density);

    }

    private void initPaint() {
        mBaseLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBaseLinePaint.setAntiAlias(true);

        mDatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDatePaint.setAntiAlias(true);

        mPathMemberPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPathMemberPaint.setAntiAlias(true);
        mPathMemberPaint.setStrokeJoin(Paint.Join.ROUND);
        mPathMemberPaint.setStyle(Paint.Style.STROKE);
        mPathMemberPaint.setStrokeWidth(2 * scaleDensity);
//        mPath.lineTo(end.x, end.y);
        mPathMemberPaint.setColor(colorMember);

        mPathOrderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPathOrderPaint.setAntiAlias(true);
        mPathOrderPaint.setStrokeJoin(Paint.Join.ROUND);
        mPathOrderPaint.setStyle(Paint.Style.STROKE);
        mPathOrderPaint.setStrokeWidth(2 * scaleDensity);
//        mPath.lineTo(end.x, end.y);
        mPathOrderPaint.setColor(colorOrder);
    }

    private void initPath() {
        mPathMember = new Path();
        mPathOrder = new Path();
        mDisPathMember = new Path();
        mDisPathOrder = new Path();
    }

    private void initAnimHelper() {
        helperMember = new PathAnimHelper(this, mPathMember, mDisPathMember);
        helperOrder = new PathAnimHelper(this, mPathOrder, mDisPathOrder);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mViewWidth = getMeasuredWidth();
        mViewHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (checkData()) {
            return;
        }
        drawLines(canvas);
        canvas.drawPath(mDisPathMember, mPathMemberPaint);
        canvas.drawPath(mDisPathOrder, mPathOrderPaint);
    }

    private boolean checkData() {
        boolean flag = false;
        if (listOrder == null) {
            flag = true;
        } else if (listOrder.size() == 0) {
            flag = true;
        }
        if (listMember == null) {
            flag = true;
        } else if (listMember.size() == 0) {
            flag = true;
        }
        return flag;
    }

    public void setData(ArrayList<LineBean> list1, ArrayList<LineBean> list2, float maxData) {
        initAnimHelper();
        if (mPathMember != null) {
            mPathMember.reset();
        }
        if (mPathOrder != null) {
            mPathOrder.reset();
        }

        prepareData(list1, list2, maxData);
        startAnim();
    }

    private void prepareData(ArrayList<LineBean> list1, ArrayList<LineBean> list2, float maxData) {
        listMember = list1;
        listOrder = list2;
        amount = listMember.size();
        this.maxData = maxData;
        if (this.maxData == 0.0f) {
            this.maxData = 1;
        }
        if (listMember == null) {
            return;
        }
        initPathData();
    }

    private void initPathData() {
        amountWidth = mViewWidth / amount;
        for (int i = 0; i < amount; i++) {
            LineBean bean = listMember.get(i);
            if (i < amount - 1) {
                float inPro = bean.getValue() / maxData;
                float yAxis = (1 - inPro) * lineHeight + 40 * density;
                PointF start = new PointF(mViewWidth / amount / 2 + amountWidth * i, yAxis);

                LineBean endBean = listMember.get(i + 1);
                float inProEnd = endBean.getValue() / maxData;
                float yAxisEnd = (1 - inProEnd) * lineHeight + 40 * density;
                PointF end = new PointF(mViewWidth / amount / 2 + amountWidth * (i + 1), yAxisEnd);

                mPathMember.moveTo(start.x, start.y);
                if (end.y >= start.y) {
                    mPathMember.cubicTo((end.x - start.x) / 2 + start.x, start.y, (end.x - start.x) / 2 + start.x, end.y, end.x, end.y);
                } else {
                    mPathMember.cubicTo((end.x - start.x) / 2 + start.x, start.y, (end.x - start.x) / 2 + start.x, end.y, end.x, end.y);
                }
            }
        }
        for (int i = 0; i < amount; i++) {
            LineBean bean = listOrder.get(i);
            if (i < amount - 1) {
                float inPro = bean.getValue() / maxData;
                float yAxis = (1 - inPro) * lineHeight + 40 * density;
                PointF start = new PointF(mViewWidth / amount / 2 + amountWidth * i, yAxis);

                LineBean endBean = listOrder.get(i + 1);
                float inProEnd = endBean.getValue() / maxData;
                float yAxisEnd = (1 - inProEnd) * lineHeight + 40 * density;
                PointF end = new PointF(mViewWidth / amount / 2 + amountWidth * (i + 1), yAxisEnd);

                mPathOrder.moveTo(start.x, start.y);
                if (end.y >= start.y) {
                    mPathOrder.cubicTo((end.x - start.x) / 2 + start.x, start.y, (end.x - start.x) / 2 + start.x, end.y, end.x, end.y);
                } else {
                    mPathOrder.cubicTo((end.x - start.x) / 2 + start.x, start.y, (end.x - start.x) / 2 + start.x, end.y, end.x, end.y);
                }
            }
        }
    }

    private void startAnim() {
        initAnimHelper();
        helperMember.setInfinite(false);
        helperMember.setAnimTime(1500);
        helperMember.startAnim();

        helperOrder.setInfinite(false);
        helperOrder.setAnimTime(1500);
        helperOrder.startAnim();
    }

    private void drawLines(Canvas canvas) {
        amountWidth = mViewWidth / amount;
//        drawBaseLine(canvas);
        for (int i = 0; i < amount; i++) {
            LineBean bean = listMember.get(i);
            String text = bean.getDate();
            float textWidth = mDatePaint.measureText(text);
            drawDate(canvas, mViewWidth / amount / 2 - textWidth / 2 + amountWidth * i, chartHeight, text);
        }
        for (int i = 0; i < amount; i++) {
            LineBean bean = listMember.get(i);
            drawLines(canvas, bean, i, colorMember);
        }
        for (int i = 0; i < amount; i++) {
            LineBean bean = listOrder.get(i);
            drawLines(canvas, bean, i, colorOrder);
        }
        for (int i = 0; i < amount; i++) {
            LineBean bean = listMember.get(i);
            drawValueText(canvas, bean, i, colorMember);
        }
        for (int i = 0; i < amount; i++) {
            LineBean bean = listOrder.get(i);
            drawValueText(canvas, bean, i, colorOrder);
        }
    }

    private void drawCircle(Canvas canvas, LineBean bean, int pos, int color) {
        float inPro = bean.getValue() / maxData;
        float yAxis = (1 - inPro) * lineHeight + 40 * density;

        mBaseLinePaint.setColor(color);
        canvas.drawCircle(mViewWidth / amount / 2 + amountWidth * pos, yAxis, 6 * density, mBaseLinePaint);

//        mBaseLinePaint.setColor(Color.WHITE);
//        mBaseLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        canvas.drawCircle(mViewWidth / amount / 2 + amountWidth * pos, yAxis, 4 * density, mBaseLinePaint);

    }

//    private void drawBaseLine(Canvas canvas) {
//        mBaseLinePaint.setColor(Color.rgb(222, 222, 222));
//        canvas.drawLine(0, chartHeight, mViewWidth, chartHeight, mBaseLinePaint);
//    }

    private void drawLines(Canvas canvas, LineBean bean, int pos, int color) {
        drawCircle(canvas, bean, pos, color);

    }

    private void drawValueText(Canvas canvas, LineBean bean, int pos, int color) {
        float inPro = bean.getValue() / maxData;
        float yAxis = (1 - inPro) * lineHeight + 40 * density;
        float width = mDatePaint.measureText("" + (int) bean.getValue());
        mDatePaint.setColor(color);
        mDatePaint.setTextSize(20 * scaleDensity);
        drawProText(canvas, mViewWidth / amount / 2 + amountWidth * pos - width / 2, yAxis, "" + (int) bean.getValue(), mDatePaint);
    }


    private void drawDate(Canvas canvas, float baseX, float baseY, String text) {
        //画文字
        mDatePaint.setColor(Color.rgb(46, 46, 46));
        mDatePaint.setTextSize(20 * scaleDensity);
        Paint.FontMetrics fontMetrics = mDatePaint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight - fontMetrics.bottom;
        float newY = baseY + offY;

        canvas.drawText(text, baseX, newY, mDatePaint);
    }


    private void drawProText(Canvas canvas, float baseX, float baseY, String text, Paint paint) {
//        mDatePaint.setColor(Color.rgb(11, 11, 11));
        //画文字
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight - fontMetrics.bottom;
        float newY = baseY - offY;
        text = BizUtil.convertBigNumber(text, 2);
        canvas.drawText(text, baseX, newY, paint);
    }

}
